package com.bdjk.bdmw.InfoGath.exception;

import org.springframework.stereotype.Component;

import com.bdjk.bdmw.InfoGath.common.constants.ExceptionConstants;


/**
 * @Package com.bdjk.bdmw.exception
 * @Description: 异常工具类，用来抛出业务异常
 * @author huangyu
 * @date 2014-11-27 上午10:04:03
 */
@Component
public class ExceptionUtil {

	/**
	 * @Description: 抛出业务异常，主要使用异常转换,捕获dao，
	 * 				 service和controller的异常，然后转换成我们定义的businessException 
	 * @param oriException
	 * @param errCode
	 * @param errMsg
	 * @throws BusinessException
	 */
	public void throwBusinessException(Throwable oriException, String errCode)
			throws BusinessException {
		
		if(oriException instanceof BusinessException){
			// 如果本身就是BusinessException，那么就直接抛出
			throw (BusinessException)oriException;
		} else {
			// 通过异常编码获取异常信息
			String errMsg = ExceptionConstants.ERROR_MAP.get(errCode);
			// 创建新的业务异常
			BusinessException businessException = new BusinessException(errCode,errMsg);
			// 把源异常的堆栈信息复制给新异常堆栈
			businessException.setStackTrace(oriException.getStackTrace());
			
			throw businessException;
		}
		
	}

}
